<?php
namespace Common\Behavior;

class CorsBehavior extends \Think\Behavior
{
    
    public function run(&$params)
    {
        /**
         * 大部分跨类库(webuploader axios)在跨域访问时会先改善OPTIONS请求来判断该api是否支持跨域
         **/
        if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
            $crosDomains = C('CORS_DOMAINS');
            if (in_array($_SERVER['HTTP_ORIGIN'], $crosDomains)) {
                header('Access-Control-Allow-Methods:OPTIONS,POST,GET,PUT,DELETE');
                header('Access-Control-Allow-Headers:Content-Type, Access-Control-Allow-Headers, Authorization');
                header('Access-Control-Allow-Origin:*');
                exit;
            }
        }
    }
}
